package com.yc.ui.admin;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.Idao.GoodsIDao;
import com.yc.dao.GoodsDao;
import com.yc.ui.MainUI;
import com.yc.ui.dialog.GoodslistDialog;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class AdminLoginItem extends Composite {
	private Table table;
	private Text text_l_id;
	private Text text_c_name;
	private Text text_r_id;
	Combo combo_choice;
	GoodsIDao dao = new GoodsDao();
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public AdminLoginItem(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashForm = new SashForm(this, SWT.VERTICAL);

		Composite composite = new Composite(sashForm, SWT.NONE);
		composite.setBackgroundImage( SWTResourceManager.getImage(MainUI.class, "/com/yc/images/主界面按钮框.PNG") );
		SWTUtil.imagetSize(composite);
		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setBounds(37, 40, 76, 20);
		lblNewLabel.setText("查询方式：");

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.setBounds(776, 35, 98, 30);
		btnNewButton.setText("查询");

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.setBounds(776, 82, 98, 30);
		btnNewButton_1.setText("删除记录");

		Label lblNewLabel_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1.setText("住宿编号：");
		lblNewLabel_1.setBounds(316, 40, 84, 20);

		text_l_id = new Text(composite, SWT.BORDER);
		text_l_id.setBounds(406, 37, 115, 26);

		combo_choice = new Combo(composite, SWT.NONE);
		combo_choice.setItems(new String[] {"按所有查询", "按住宿编号查询", "按房间号查询", "按姓名查询"});
		combo_choice.setBounds(139, 37, 148, 28);
		combo_choice.select(0);

		Label lblNewLabel_2 = new Label(composite, SWT.NONE);
		lblNewLabel_2.setBounds(37, 111, 105, 20);
		lblNewLabel_2.setText("查询结果如下：");

		Label lblNewLabel_1_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1_1.setText("姓名：");
		lblNewLabel_1_1.setBounds(549, 40, 51, 20);

		text_c_name = new Text(composite, SWT.BORDER);
		text_c_name.setBounds(606, 37, 115, 26);

		Label lblNewLabel_1_2 = new Label(composite, SWT.NONE);
		lblNewLabel_1_2.setText("房间号：");
		lblNewLabel_1_2.setBounds(316, 87, 84, 20);

		text_r_id = new Text(composite, SWT.BORDER);
		text_r_id.setBounds(406, 84, 115, 26);

		Composite composite_1 = new Composite(sashForm, SWT.NONE);
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));

		table = new Table(composite_1, SWT.BORDER | SWT.FULL_SELECTION);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				GoodslistDialog g = new GoodslistDialog(getShell(), SWT.NONE);
				g.open(table);
			}
		});
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(116);
		tblclmnNewColumn.setText("住宿编号");

		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(95);
		tblclmnNewColumn_1.setText("姓名");

		TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_3.setWidth(88);
		tblclmnNewColumn_3.setText("房间号");

		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("房间类型");

		TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_4.setWidth(113);
		tblclmnNewColumn_4.setText("入住时间");

		TableColumn tblclmnNewColumn_5 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_5.setWidth(110);
		tblclmnNewColumn_5.setText("退房时间");

		TableColumn tblclmnNewColumn_6 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_6.setWidth(112);
		tblclmnNewColumn_6.setText("住宿费");

		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(100);
		tblclmnNewColumn_2.setText("其他消费");

		TableColumn tblclmnNewColumn_7 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_7.setWidth(128);
		tblclmnNewColumn_7.setText("总消费金额");
		sashForm.setWeights(new int[] {141, 464});
		composite_1.setBackgroundMode(SWT.INHERIT_FORCE);
		SWTUtil.imagetSize(composite_1);
		
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String info = combo_choice.getText();//下拉框的选项

				Map<String,Object> map = new HashMap<String,Object>();
				if("按所有查询".equals(info)){
					//在表格中添加所查记录
					showMessage(map);
				}
				if("按住宿编号查询".equals(info)){
					map.put("l_id", text_l_id.getText());
					showMessage(map);
				}
				if("按房间号查询".equals(info)){
					map.put("r_id", text_r_id.getText());
					showMessage(map);
				}
				map.put("c_name", text_c_name.getText());
				showMessage(map);
			}
		});

	}
	public void showMessage(Map<String, Object> map) {
		//清除表格
		table.removeAll();
		//查询信息
		List<Map<String, Object>> list = dao.findGoodsList(map);

		TableItem tableItem = null;//表格一行记录
		//判空
		if(null == list || list.isEmpty()) {
			StringUtil.showMessageBox(getShell(), "查无消费信息");
			return;
		}
		//有信息 循环

		for(Map<String, Object> m : list) {
			System.out.println(StringUtil.toString(m.get("end_date")));
			if("".equals(StringUtil.toString(m.get("end_date")))) {
				continue;
			}
			tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(new String[] {
					StringUtil.toString(m.get("l_id")),
					StringUtil.toString(m.get("c_name")),
					StringUtil.toString(m.get("r_id")),
					StringUtil.toString(m.get("r_type")),
					StringUtil.toString(m.get("start_date")),                  
					StringUtil.toString(m.get("end_date")),
					StringUtil.toString(m.get("avg(l.l)")),
					StringUtil.toString(m.get("sum(l.f)")),
					StringUtil.toString(m.get("totalprice")),
			});
		}
	}
}
